package com.lanren.common.widget.pickerview;

import android.app.Activity;
import android.view.View;

import com.lanren.common.R;
import com.lanren.common.widget.pickerview.config.PickerConfig;
import com.lanren.common.widget.pickerview.data.Type;
import com.lanren.common.widget.pickerview.data.WheelCalendar;
import com.lanren.common.widget.pickerview.listener.OnDateSetListener;
import com.lanren.easydialog.DialogViewHolder;
import com.lanren.easydialog.EasyDialog;

import java.util.Calendar;

/**
 * @ClassName TimePickerDialog
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2020/3/23 20:51
 * @Version 1.0
 */
public class TimePickerDialog {

    PickerConfig mPickerConfig;
    private TimeWheel mTimeWheel;
    private long mCurrentMillSeconds;
    private EasyDialog dialog;

    private static TimePickerDialog newIntance(PickerConfig pickerConfig) {
        TimePickerDialog timePickerDialog = new TimePickerDialog();
        timePickerDialog.initPickerConfig(pickerConfig);
        return timePickerDialog;
    }

    public void initPickerConfig(PickerConfig pickerConfig) {
        this.mPickerConfig = pickerConfig;
    }

    public void show(Activity activity) {
        if (dialog == null) {
            dialog = new EasyDialog(activity, R.layout.timepicker_layout) {
                @Override
                public void onBindViewHolder(DialogViewHolder holder) {
                    holder.setText(R.id.tv_cancel, mPickerConfig.mCancelString);
                    holder.setText(R.id.tv_sure, mPickerConfig.mSureString);
                    holder.setText(R.id.tv_title, mPickerConfig.mTitleString);
                    holder.getView(R.id.toolbar).setBackgroundColor(mPickerConfig.mThemeColor);

                    mTimeWheel = new TimeWheel(holder.getConvertView(), mPickerConfig);

                    holder.setOnClick(R.id.tv_cancel, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                        }
                    }).setOnClick(R.id.tv_sure, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            sureClicked();
                            dismiss();
                        }
                    });
                }
            }
                    .backgroundLight(0.1)
                    .setCancelAble(true)
                    .setCanceledOnTouchOutside(true)
                    .fromBottom()
                    .fullWidth()
                    .showDialog();
        } else {
            dialog.showDialog();
        }
    }

    /*
     * @desc This method returns the current milliseconds. If current milliseconds is not set,
     *       this will return the system milliseconds.
     * @param none
     * @return long - the current milliseconds.
     */
    public long getCurrentMillSeconds() {
        if (mCurrentMillSeconds == 0)
            return System.currentTimeMillis();

        return mCurrentMillSeconds;
    }

    /*
     * @desc This method is called when onClick method is invoked by sure button. A Calendar instance is created and
     *       initialized.
     * @param none
     * @return none
     */
    void sureClicked() {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();

        calendar.set(Calendar.YEAR, mTimeWheel.getCurrentYear());
        calendar.set(Calendar.MONTH, mTimeWheel.getCurrentMonth() - 1);
        calendar.set(Calendar.DAY_OF_MONTH, mTimeWheel.getCurrentDay());
        calendar.set(Calendar.HOUR_OF_DAY, mTimeWheel.getCurrentHour());
        calendar.set(Calendar.MINUTE, mTimeWheel.getCurrentMinute());

        mCurrentMillSeconds = calendar.getTimeInMillis();
        if (mPickerConfig.mCallBack != null) {
            mPickerConfig.mCallBack.onDateSet(this, mCurrentMillSeconds);
        }
    }

    public static class Builder {
        PickerConfig mPickerConfig;

        public Builder() {
            mPickerConfig = new PickerConfig();
        }

        public Builder setType(Type type) {
            mPickerConfig.mType = type;
            return this;
        }

        public Builder setThemeColor(int color) {
            mPickerConfig.mThemeColor = color;
            return this;
        }

        public Builder setCancelStringId(String left) {
            mPickerConfig.mCancelString = left;
            return this;
        }

        public Builder setSureStringId(String right) {
            mPickerConfig.mSureString = right;
            return this;
        }

        public Builder setTitleStringId(String title) {
            mPickerConfig.mTitleString = title;
            return this;
        }

        public Builder setToolBarTextColor(int color) {
            mPickerConfig.mToolBarTVColor = color;
            return this;
        }

        public Builder setWheelItemTextNormalColor(int color) {
            mPickerConfig.mWheelTVNormalColor = color;
            return this;
        }

        public Builder setWheelItemTextSelectorColor(int color) {
            mPickerConfig.mWheelTVSelectorColor = color;
            return this;
        }

        public Builder setWheelItemTextSize(int size) {
            mPickerConfig.mWheelTVSize = size;
            return this;
        }

        public Builder setCyclic(boolean cyclic) {
            mPickerConfig.cyclic = cyclic;
            return this;
        }

        public Builder setMinMillseconds(long millseconds) {
            mPickerConfig.mMinCalendar = new WheelCalendar(millseconds);
            return this;
        }

        public Builder setMaxMillseconds(long millseconds) {
            mPickerConfig.mMaxCalendar = new WheelCalendar(millseconds);
            return this;
        }

        public Builder setCurrentMillseconds(long millseconds) {
            mPickerConfig.mCurrentCalendar = new WheelCalendar(millseconds);
            return this;
        }

        public Builder setYearText(String year) {
            mPickerConfig.mYear = year;
            return this;
        }

        public Builder setMonthText(String month) {
            mPickerConfig.mMonth = month;
            return this;
        }

        public Builder setDayText(String day) {
            mPickerConfig.mDay = day;
            return this;
        }

        public Builder setHourText(String hour) {
            mPickerConfig.mHour = hour;
            return this;
        }

        public Builder setMinuteText(String minute) {
            mPickerConfig.mMinute = minute;
            return this;
        }

        public Builder setCallBack(OnDateSetListener listener) {
            mPickerConfig.mCallBack = listener;
            return this;
        }

        public TimePickerDialog build() {
            return newIntance(mPickerConfig);
        }
    }

    public void clearDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
